package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDictType;
import com.ruoyi.system.service.IDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/dicts/type")
public class DictTypeController extends BaseController
{
    @Autowired
    private IDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDictType dictType)
    {
        startPage();
        LambdaQueryWrapper<SysDictType> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dictType.getDictName()),SysDictType::getDictName,dictType.getDictName());
        wrapper.eq(StringUtils.isNotBlank(dictType.getStatus()),SysDictType::getStatus,dictType.getStatus());
        wrapper.like(StringUtils.isNotBlank(dictType.getDictType()),SysDictType::getDictType,dictType.getDictType());
        if(dictType.getParams().size()>0){
            wrapper.between(dictType.getParams().get("beginTime")!=null && dictType.getParams().get("endTime")!=null,SysDictType::getCreateTime,dictType.getParams().get("beginTime"),dictType.getParams().get("endTime"));
        }
        List<SysDictType> list = dictTypeService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDictType dict)
    {
        dict.setCreateTime(new Date());
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.save(dict));
    }


    /**
     * 查询字典类型详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId)
    {
        return success(dictTypeService.getById(dictId));
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictType dict)
    {
        dict.setUpdateTime(new Date());
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.updateById(dict));
    }


}
